﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZG_member.BLL;
using System.Security.Cryptography;

namespace ZG_member
{
    public partial class FrontSettingForm : Form
    {
        MainForm main;
        public FrontSettingForm(MainForm mm)
        {
            InitializeComponent();
            main = mm;
            this.Opacity = 0;
        }

        private void FrontSettingForm_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            for (int i = 0; i < 101; i++)
            {
                domainUpDown1.Items.Add(i);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtwarning.Text == "")
            {
                txtwarning.Text = "0";
            }
            if (txtcomsumeactive.Text == "")
            {
                txtcomsumeactive.Text = "0";
            }
            if (domainUpDown1.Text == "")
            {
                domainUpDown1.Text = "0";
            }
            foregrounding fore = new foregrounding();
            DataTable dt = fore.GetList("").Tables[0];
            Model.foregrounding foreModel = new Model.foregrounding();

            if (dt.Rows.Count == 1)
            {
                foreModel = fore.GetModelList("")[0];
            }
            foreModel.balance = cbxbalance.Checked;
            foreModel.warningTime = decimal.Parse(txtwarning.Text);
            foreModel.comsumeactive = decimal.Parse(txtcomsumeactive.Text);
            foreModel.pointactive = cbxpointactive.Checked;
            foreModel.remind = int.Parse(domainUpDown1.Text);
            if (dt.Rows.Count == 1)
            {
                if (fore.Update(foreModel))
                {

                }
                else
                {
                    MessageBox.Show("前台设置失败!");
                }
            }
            else
            {
                int row = fore.Add(foreModel);
                if (row > 0)
                {

                }
                else
                {
                    MessageBox.Show("前台设置失败!");
                }
            }



            memberInfo info = new memberInfo();
            Model.memberInfo infoModel = new Model.memberInfo();
            if (cbxchushihuacard.Checked == true)
            {
                infoModel = info.GetModelList("")[0];
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes("000000");
                bt = md5.ComputeHash(bt);


                infoModel.pwd = BitConverter.ToString(bt).Replace("-", "");

                if (info.Update2(infoModel))
                {
                    MessageBox.Show("卡号初始化成功!");
                    main.shifouxianshi(MainForm.Operator);
                }
                else
                {
                    MessageBox.Show("卡号初始化失败!");
                }
            }
            if (cbxchushihuacard1.Checked == true)
            {
                infoModel = info.GetModelList("memberid='" + txtmenberid.Text + "'")[0];
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes("000000");
                bt = md5.ComputeHash(bt);
                infoModel.pwd = BitConverter.ToString(bt).Replace("-", "");
                if (info.Update(infoModel))
                {
                    MessageBox.Show("卡号初始化成功!");
                    main.shifouxianshi(MainForm.Operator);
                }
                else
                {
                    MessageBox.Show("卡号初始化失败!");
                }
            }

            if (cbxclearpoint.Checked == true)
            {
                infoModel = info.GetModelList("")[0];
                infoModel.point = 0;
                if (info.Update1(infoModel))
                {
                    MessageBox.Show("会员积分清空");
                    main.shifouxianshi(MainForm.Operator);
                }
                else
                {
                    MessageBox.Show("会员积分清空失败!");
                }
            }
            if (cbxmenberid1.Checked == true)
            {
                infoModel = info.GetModelList("memberid='" + txtmemberid1.Text + "'")[0];
                infoModel.point = 0;
                if (info.Update(infoModel))
                {
                    MessageBox.Show("会员积分清空");
                    main.shifouxianshi(MainForm.Operator);
                }
                else
                {
                    MessageBox.Show("会员积分清空失败!");
                }

            }
            operatorInfo operate = new operatorInfo();
            Model.operatorInfo operateModel = new Model.operatorInfo();
            if (cbxuserpwd.Checked == true)
            {
                operateModel = operate.GetModelList("")[0];
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes("admin");
                bt = md5.ComputeHash(bt);
                operateModel.password = BitConverter.ToString(bt).Replace("-", "");
                if (operate.Update1(operateModel))
                {
                    MessageBox.Show("操作员密码初始化成功!");
                }
                else
                {
                    MessageBox.Show("操作员密码初始化失败!");
                }
            }
            if (cbxusername.Checked == true)
            {
                operateModel = operate.GetModelList("username='" + txtusername.Text + "'")[0];
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes("000000");
                bt = md5.ComputeHash(bt);
                operateModel.password = BitConverter.ToString(bt).Replace("-", "");
                if (operate.Update(operateModel))
                {
                    MessageBox.Show("操作员密码初始化成功!");
                }
                else
                {
                    MessageBox.Show("操作员密码初始化失败!");
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.timer2.Start();
        }
        int flag = 0;
        private void FrontSettingForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
    }
}



           

                
         
              